package fr.pud.client.controller.query;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import fr.pud.client.controller.InputCheck;
import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.images.ImagesDatabase;
import fr.pud.client.model.Queries;
import fr.pud.client.view.MainFrame;
import fr.pud.client.view.QueryFrame;

/**
 * The controller to open the query frame.
 * Apr 7, 2012 - Projet_Ose
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         OpenQueryFrame.java
 */
public class OpenQueryFrame extends AbstractAction {
    /**
     * The default serial UID
     */
    private static final long serialVersionUID = 8757712103293651503L;

    /**
     * @param quantity
     * @param maxDriveduration
     * @param openingTimePickup
     * @param closingTimePickup
     * @param openingTimeDelivery
     * @param closingTimeDelivery
     * @return true if all the inputs have the correct format. If not, display a
     *         message with all the errors.
     */
    public static boolean checkInputUser(String quantity,
            String maxDriveduration, String loadingTimePickUp,
            String loadingTimeDelivery, String openingTimePickup,
            String closingTimePickup, String openingTimeDelivery,
            String closingTimeDelivery) {
        boolean toReturn = true;
        String toDisplay = "An error occured, please check : ";
        if (!quantity.matches(InputCheck.NUMBER_ONLY)) {
            toDisplay += "\nCapacity is not a number";
            toReturn = false;
        }
        if (!maxDriveduration.matches(InputCheck.NUMBER_ONLY)) {
            toDisplay += "\nMax drive duration is not a number";
            toReturn = false;
        }
        if (!loadingTimePickUp.matches(InputCheck.NUMBER_ONLY)) {
            toDisplay += "\nloading Time for pickup is not a number";
            toReturn = false;
        }
        if (!loadingTimeDelivery.matches(InputCheck.NUMBER_ONLY)) {
            toDisplay += "\nloading Time for delivery is not a number";
            toReturn = false;
        }
        if (!openingTimeDelivery.matches(InputCheck.HOURS)) {
            toDisplay += "\nOpening time  is not a time format";
            toReturn = false;
        }
        if (!openingTimePickup.matches(InputCheck.HOURS)) {
            toDisplay += "\nOpening time  is not a time format";
            toReturn = false;
        }
        if (!closingTimeDelivery.matches(InputCheck.HOURS)) {
            toDisplay += "\nOpening time  is not a time format";
            toReturn = false;
        }
        if (!closingTimePickup.matches(InputCheck.HOURS)) {
            toDisplay += "\nOpening time  is not a time format";
            toReturn = false;
        }
        if (!toReturn) {
            JOptionPane.showMessageDialog(QueryFrame.getInstance(), toDisplay,
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
        return toReturn;
    }

    /**
     * Default constructor
     */
    public OpenQueryFrame() {
        this.putValue(NAME, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.CREATE_QUERY));
        this.putValue(SHORT_DESCRIPTION, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.CREATE_QUERY));
        this.putValue(SMALL_ICON, ImagesDatabase.ROAD);
    }

    /**
     * Opens the query frame which is represented by a singleton.
     */
    @Override
    public void actionPerformed(ActionEvent aArg0) {
        QueryFrame.getInstance().setVisible(true);
        QueryFrame.getInstance().getDataPanel().getPUDStationDelivery()
                .addItemListener(new ItemListennerForDelivery());
        QueryFrame.getInstance().getDataPanel().getPUDStationPickUp()
                .addItemListener(new ItemListennerForPickUp());
        MainFrame.getMainFrame().setVisible(true);
        MainFrame.getMainFrame().setEnabled(false);
        if (Queries.getInstance().getQueries().size() == 0) {
            QueryFrame.getInstance().getButtonModify().setVisible(false);
            QueryFrame.getInstance().getButtonDelete().setVisible(false);
        }
        else {
            QueryFrame.getInstance().getButtonModify().setVisible(true);
            QueryFrame.getInstance().getButtonDelete().setVisible(true);
        }
    }
}
